import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author gao
 * @date 2025/8/29 14:26
 */

public class Solution {
    public int findShortestSubArray(int[] nums) {
        Map<Integer, Integer> count = new HashMap<>();
        Map<Integer, Integer> firstIndex = new HashMap<>();
        Map<Integer, Integer> lastIndex = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            // 更新频率
            count.put(num, count.getOrDefault(num, 0) + 1);

            // 记录第一次出现的位置
            if (!firstIndex.containsKey(num)) {
                firstIndex.put(num, i);
            }

            // 更新最后一次出现的位置
            lastIndex.put(num, i);
        }

        // 找出数组的度
        int degree = 0;
        for (int freq : count.values()) {
            degree = Math.max(degree, freq);
        }

        // 计算最短子数组的长度
        int minLen = Integer.MAX_VALUE;
        for (int num : count.keySet()) {
            if (count.get(num) == degree) {
                int len = lastIndex.get(num) - firstIndex.get(num) + 1;
                minLen = Math.min(minLen, len);
            }
        }
        return minLen;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.findShortestSubArray(nums));
        }
    }
}